package com.zyw.customcalendar.view;

import android.content.Context;
import android.support.v4.view.NestedScrollingParent;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.OverScroller;

/**
 * Created by zyw on 2017/9/14.
 */
public class NestedScrollLayout extends FrameLayout implements NestedScrollingParent {

    private int mTopHeight;
    private View mTopView;
    private OverScroller mScroller;
    private View mContentView;

    public NestedScrollLayout(Context context) {
        super(context);


        mScroller = new OverScroller(context);
    }

    public NestedScrollLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);


        mScroller = new OverScroller(context);
    }

    public NestedScrollLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);


        mScroller = new OverScroller(context);
    }

    public NestedScrollLayout(Context context, AttributeSet attrs) {
        super(context, attrs);


        mScroller = new OverScroller(context);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        findChildView();
    }

    private void findChildView(){
        int childCount = getChildCount();

        if(childCount > 0){
            mTopView = getChildAt(0);
            Log.i("zyw","childCount = " + childCount);
        }

        if(childCount > 1){
            mContentView = getChildAt(1);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        int contentTop = t + mTopHeight + getPaddingTop();
        int contentBottom = b + mTopHeight;
        mContentView.layout(l, contentTop, r, contentBottom);
        Log.i("zyw", "onLayout = ");
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);


        mTopHeight = mTopView.getHeight();
        Log.i("zyw", "onMeasure = "+mTopHeight);
    }

    @Override
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes)
    {
        Log.i("zyw", "onStartNestedScroll = ");
        return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
    }
    @Override
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed)
    {
        Log.i("zyw", "onNestedPreScroll = ");
        boolean hiddenTop = dy > 0 && getScrollY() < mTopHeight;
        boolean showTop = dy < 0 && getScrollY() > 0 && !ViewCompat.canScrollVertically(target, -1);

        if (hiddenTop || showTop)
        {
            scrollBy(0, dy);
            consumed[1] = dy;
        }
    }

    @Override
    public boolean onNestedPreFling(View target, float velocityX, float velocityY)
    {
        Log.i("zyw", "onNestedPreFling =");
        return true;
    }

    @Override
    public void onStopNestedScroll(View target){
        Log.i("zyw", "onStopNestedScroll = srcoll y = " + getScrollY());
        if(getScrollY() > mTopHeight / 2){
            mScroller.startScroll(0, 0, 0, mTopHeight-getScrollY(), 300);
        }else{
            mScroller.startScroll(0, 0, 0, getScrollY()-mTopHeight, 300);
        }
    }

    public void fling(int velocityY)
    {
        mScroller.fling(0, getScrollY(), 0, velocityY, 0, 0, 0, mTopHeight);
        invalidate();
    }

    public void computeScroll()
    {
        if (mScroller.computeScrollOffset())//如果mScroller没有调用startScroll，这里将会返回false。
        {
            //因为调用computeScroll函数的是MyLinearLayout实例，
            //所以调用scrollTo移动的将是该实例的孩子，也就是MyButton实例
            scrollBy(0, mScroller.getCurrY());

            //继续让系统重绘
            //postInvalidate();
        }
    }

    @Override
    public void scrollTo(int x, int y)
    {
        if (y < 0)
        {
            y = 0;
        }
        if (y > mTopHeight)
        {
            y = mTopHeight;
        }
        if (y != getScrollY())
        {
            super.scrollTo(x, y);
        }
    }
}
